package com.yuyou.fn.educational.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.Campus;
import com.yuyou.fn.educational.entity.SummerWinterDate;
import com.yuyou.fn.educational.mapper.SummerWinterDateMapper;
import com.yuyou.fn.educational.service.ICampusService;
import com.yuyou.fn.educational.service.IRoomPriceService;
import com.yuyou.fn.educational.service.ISummerWinterDateService;
import com.yuyou.fn.educational.vo.SummerWinterDateVo;
import com.yuyou.fn.platform.entity.AreaTeam;
import com.yuyou.fn.platform.service.IAreaTeamService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 *
 * SummerWinterDate 表数据服务层接口实现类
 *
 */
@Service
public class SummerWinterDateServiceImpl extends SuperServiceImpl<SummerWinterDateMapper, SummerWinterDate,Long> implements ISummerWinterDateService {

    @Resource
    private IRoomPriceService roomPriceService;

    @Resource
    private ICampusService campusService;

    @Resource
    private IAreaTeamService areaTeamService;

    @Override
    @Transactional
    public void saveOrUpdate(SummerWinterDateVo summerWinterDateVo) {
        if (summerWinterDateVo.getAreaTeamId()==null || StringUtils.isEmpty(summerWinterDateVo.getYear())){
            throw new BusException("区域和年份必填");
        }
        summerWinterDateVo.setYear(summerWinterDateVo.getYear().trim());
        if (summerWinterDateVo.getSummerWinterDateId()==null){
            SummerWinterDate checkSummerWinterDate=this.findByAreaTeamIdAndYear(summerWinterDateVo.getAreaTeamId(),summerWinterDateVo.getYear());
            if (checkSummerWinterDate!=null){
                throw new BusException("该区域该年份的寒暑假时间段已经存在，请前往修改");
            }
            SummerWinterDate summerWinterDate=new SummerWinterDate();
            BeanUtils.copyProperties(summerWinterDate,summerWinterDateVo);
            summerWinterDate.setUpdateTime(TimeUtils.now());
            summerWinterDate.setCreateTime(TimeUtils.now());
            this.checkDate(summerWinterDate);
            this.insert(summerWinterDate);
        }else{
            SummerWinterDate check=this.findById(summerWinterDateVo.getSummerWinterDateId());
            if (check!=null && !check.getYear().equals(summerWinterDateVo.getYear())){
                throw  new BusException("年份不可以修改，请前往添加");
            }
            if (check!=null && !check.getAreaTeamId().equals(summerWinterDateVo.getAreaTeamId())){
                throw  new BusException("区域不可以修改，请前往添加");
            }
            SummerWinterDate summerWinterDate=new SummerWinterDate();
            BeanUtils.copyProperties(summerWinterDate,summerWinterDateVo);
            summerWinterDate.setUpdateTime(TimeUtils.now());
            this.checkDate(summerWinterDate);
            this.updateSelectiveById(summerWinterDate);
            roomPriceService.changeDateBySummerWinterDate(summerWinterDate);
        }
    }
    private  void checkDate(SummerWinterDate summerWinterDate){
        if (summerWinterDate.getSummerStartDate().getTime()>summerWinterDate.getSummerEndDate().getTime()){
            throw new BusException("暑假开始日期必须小于结束日期");
        }
        if (summerWinterDate.getWinterStartDate().getTime()>summerWinterDate.getWinterEndDate().getTime()){
            throw new BusException("寒假开始日期必须小于结束日期");
        }
        if(summerWinterDate.getWinterEndDate().getTime()>=summerWinterDate.getSummerStartDate().getTime()){
            throw new BusException("寒假结束日期必须小于暑假开始日期");
        }
        String summerStartYear= Utils.formatDate(summerWinterDate.getSummerStartDate(),"yyyy");
        String summerEndYear=Utils.formatDate(summerWinterDate.getSummerEndDate(),"yyyy");
        String winterStartYear=Utils.formatDate(summerWinterDate.getWinterStartDate(),"yyyy");
        String winterEndYear=Utils.formatDate(summerWinterDate.getSummerEndDate(),"yyyy");
        String year=summerWinterDate.getYear();
        if (!year.equals(summerStartYear) || !year.equals(summerEndYear) || !year.equals(winterStartYear) || !year.equals(winterEndYear)){
            throw  new BusException("存在日期不在选择的年份里");
        }
    }

    @Override
    public SummerWinterDate findByAreaTeamIdAndYear(Long areaTeamId, String year) {
        Example example=SummerWinterDate.createExample();
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("areaTeamId",areaTeamId);
        criteria.andEqualTo("year",year);

        return findOneByExample(example);
    }

    @Override
    public List<SummerWinterDate> findByYear(String year,List<Long> areaTeamIdList) {
        List<AreaTeam> areaTeamList=areaTeamService.findByIds(areaTeamIdList);
        Example example=SummerWinterDate.createExample();
        Example.Criteria criteria=example.createCriteria();
        if (StringUtils.isNotEmpty(year)){
            criteria.andEqualTo("year",year);
        }
        criteria.andIn("areaTeamId",areaTeamIdList);
        example.orderBy("createTime").desc();
        List<SummerWinterDate> summerWinterDateList=findListByExample(example);
        for (SummerWinterDate summerWinterDate:summerWinterDateList){
            for (AreaTeam areaTeam:areaTeamList){
                if (summerWinterDate.getAreaTeamId().equals(areaTeam.getAreaTeamId())){
                    summerWinterDate.setAreaTeamName(areaTeam.getName());
                    break;
                }
            }
        }
        return summerWinterDateList;
    }

    @Override
    public SummerWinterDate findByCampusIdAndYear(Long campusId, String year) {
        Campus campus=campusService.findById(campusId);
        return this.findByAreaTeamIdAndYear(campus.getAreaTeamId(),year);
    }

    @Override
    public List<SummerWinterDate> findByAreaTeamIdAndYears(Long areaTeamId, List<String> years) {
        if(areaTeamId==null || years==null || years.size()<=0){
            return Collections.EMPTY_LIST;
        }
        Example example=SummerWinterDate.createExample();
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("areaTeamId",areaTeamId);
        criteria.andIn("year",years);

        return findListByExample(example);
    }
}